#include "qeditablefield.h"

QEditableField::QEditableField(QString title,QString value,QWidget *parent)
   : QWidget(parent)
{
	ui.setupUi(this);
	normalState();
	m_normalizedValue = value;
	m_normalizedValue.remove("\"").remove(EXP_UNKNOWN);
	ui.title->setText(title);
	ui.field->setText(m_normalizedValue);
	m_save = value;
	connect(ui.accept,SIGNAL(clicked()),this,SLOT(accepted()));
	connect(ui.refuse,SIGNAL(clicked()),this,SLOT(refused()));
	connect(ui.field,SIGNAL(textEdited(const QString&)),this,SLOT(editState()));
	connect(ui.field,SIGNAL(returnPressed()),this,SLOT(accepted()));
}

QEditableField::~QEditableField()
{

}


void QEditableField::accepted()
{
	normalState();
	m_save = ui.field->text();
	m_normalizedValue = m_save;
	m_normalizedValue.remove("\"").remove(EXP_UNKNOWN);
	emit hasChanged(m_save);
}

void QEditableField::refused()
{
	normalState();
	ui.field->setText(m_normalizedValue);
}

void QEditableField::editState()
{
	if(ui.accept->isHidden())
	{
		ui.field->setMinimumWidth(ui.field->minimumWidth() - 2*ui.accept->minimumWidth());
		ui.field->setMaximumWidth(ui.field->maximumWidth() - 2*ui.accept->maximumWidth());
		ui.accept->show();
		ui.refuse->show();
	}
}

void QEditableField::normalState()
{
	if(!ui.accept->isHidden())
	{
		ui.field->setMinimumWidth(ui.field->minimumWidth() + 2*ui.accept->minimumWidth());
		ui.field->setMaximumWidth(ui.field->maximumWidth() + 2*ui.accept->maximumWidth());
		ui.accept->hide();
		ui.refuse->hide();
	}
}
void QEditableField::set(QString title,QString value) 
{ 
	m_normalizedValue = value;
	m_normalizedValue.remove("\"").remove(EXP_UNKNOWN);
	ui.title->setText(title);
	ui.field->setText(m_normalizedValue);
	m_save = value;
	normalState();
}